package controller;

import implementor.BalanceImpl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import javax.swing.table.DefaultTableModel;

import daoInterface.BalanceDao;

import model.Balance;

import utilities.AppUtilites;
import utilities.ViewUtilites;
import view.balance.BalanceReport;

public class BalanceReportController implements ActionListener{
	private BalanceReport view;
	private Balance model;
	
	public BalanceReportController() {
	
	}

	public BalanceReportController(BalanceReport view, Balance model) {
		super();
		this.view = view;
		this.model = model;
		view.actionlisteners(this);
		AppUtilites.setSellerNameCombo(view.getSellercomboBox());
	}

	public BalanceReport getView() {
		return view;
	}

	public void setView(BalanceReport view) {
		this.view = view;
	}

	public Balance getModel() {
		return model;
	}

	public void setModel(Balance model) {
		this.model = model;
	}
	
	public void reactOnApplyFilter() throws Exception
	{
		Date fromdate=ViewUtilites.convert(view.getLblClick().getText());
		Date todate=ViewUtilites.convert(view.getLblClick_1().getText());
		String sellername=view.getSellercomboBox().getSelectedItem().toString();
		BalanceDao bd=new BalanceImpl();
		Balance[] b=bd.retrieveOnMultipleCond(fromdate, todate, sellername);
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		
		for(int i=0;i<b.length;i++)
		{
			//dm.addRow(new Object[]{b[i].getSellerName(),b[i].getPurchaseDate(),b[i].getTotalAmt(),b[i].getAmtPaid()});
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String k=e.getActionCommand();
		
		if(k.contains("APPLY FILTER"))
		{
			try {
				reactOnApplyFilter();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		if(k.contains("CLICK"))
		{
			ViewUtilites.makeDateComponent(view.getLblClick());
		}	
		
		if(k.contains("CLICK"))
		{
			ViewUtilites.makeDateComponent(view.getLblClick_1());
		}	
		
	}
	
	

}
